package com.atguigu.es.demo;

import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.DocumentResult;
import io.searchbox.core.Index;

import java.io.IOException;

/**
 * Created by Smexy on 2021/7/9
 *
 *

 */
public class WriteDemo4 {

    public static void main(String[] args) throws IOException {

        //创建一个 JestClientFactory
        JestClientFactory jestClientFactory = new JestClientFactory();

        HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://hadoop102:9200").build();

        jestClientFactory.setHttpClientConfig(httpClientConfig);

        // 从工厂中获取一个 客户端对象
        JestClient jestClient = jestClientFactory.getObject();


        String str="{\n" +
                "  \"id\":5,\n" +
                "  \"name\":\"《翠花洗酸菜》\"\n" +
                "}";

        // 执行向  movie_index 插入
        Index index2 = new Index.Builder(str)
                .type("movie_type")
                .index("movie_index")
                .id("6")
                .build();

        DocumentResult execute = jestClient.execute(index2);

        System.out.println(execute.getResponseCode());

        System.out.println(execute.getErrorMessage());


        //关闭客户端
        jestClient.close();


    }
}
